import { defineStore } from 'pinia';
import Cookies from 'js-cookie';
import { Session } from '/@/utils/storage';

/**
 * 用户信息
 * @methods setUserInfos 设置用户信息
 */

export const useUserInfo = defineStore('userInfo', {

	state: () => ({
		userinfo: {
			id: 0,
			imgs: '',
			account: ''
		},
		url: '',
		menuList: []
	}),
	actions: {

		setUserInfos(data: any) {

			this.$patch((state) => {
				state.userinfo = data.info;
				state.url = data.url;
				data.list.forEach((item: any) => {
					if (item.path === '/') {
						item.path = '/staff'
					}
					if (item.children.length === 1 && item.path === item.children[0].path) {
						delete item.children;
					}
				});
				state.menuList = data.list;
			})
		},
	},
	getters: {
		// 添加获取用户ID的getter，用于判断是否已登录
		userId(): number {
			return this.userinfo?.id || 0;
		},
		isLoggedIn(): boolean {
			return !!(this.userinfo?.id);
		}
	},
	persist: {
		key: 'userInfo',
		storage: localStorage,
	}
});